package com.luoxiang.socialandroid.activity;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.ACCID;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.HOST;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.HOSTAUDIT;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.KEY;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.ClipboardUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.luoxiang.lib_common.utils.MmkvUtil;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.adapter.GiftsAdapter;
import com.luoxiang.socialandroid.adapter.GrbqAdapter;
import com.luoxiang.socialandroid.adapter.RvgiftAdapter;
import com.luoxiang.socialandroid.adapter.SingleImageAdapter;
import com.luoxiang.socialandroid.adapter.YhpjAdapter;
import com.luoxiang.socialandroid.base.BaseActivity;
import com.luoxiang.socialandroid.bean.BlockBean;
import com.luoxiang.socialandroid.bean.GetMessageBean;
import com.luoxiang.socialandroid.bean.GetWhereBean;
import com.luoxiang.socialandroid.bean.NumberBean;
import com.luoxiang.socialandroid.bean.OurinfoBean;
import com.luoxiang.socialandroid.bean.RewardBean;
import com.luoxiang.socialandroid.bean.UserDynamicBean;

import com.luoxiang.socialandroid.bean.UserHome1Bean;
import com.luoxiang.socialandroid.bean.UserInfo1Bean;
import com.luoxiang.socialandroid.dialog.Bottomsheet6Dialog;
import com.luoxiang.socialandroid.dialog.TipDialog;
import com.luoxiang.socialandroid.dialog.WeChatDialog;
import com.luoxiang.socialandroid.utils.AESUtil;
import com.luoxiang.socialandroid.utils.CustomClickListener;
import com.luoxiang.socialandroid.utils.NetUtils;
import com.luoxiang.socialandroid.utils.NetUtils2;
import com.lxj.xpopup.XPopup;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.msg.MessageBuilder;
import com.netease.nimlib.sdk.msg.MsgService;
import com.netease.nimlib.sdk.msg.attachment.MsgAttachment;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.netease.yunxin.kit.chatkit.ui.custom.GiftAttachment;
import com.netease.yunxin.kit.chatkit.ui.dialog.GiftDialog;
import com.netease.yunxin.kit.corekit.im.model.UserInfo;
import com.netease.yunxin.kit.corekit.im.utils.RouterConstant;
import com.netease.yunxin.kit.corekit.route.XKitRouter;
import com.netease.yunxin.nertc.ui.CallKitUI;
import com.netease.yunxin.nertc.ui.base.CallParam;
import com.stx.xhb.xbanner.XBanner;
import com.stx.xhb.xbanner.transformers.Transformer;

import java.util.List;
import java.util.UUID;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;

public class DetailsActivity extends BaseActivity {
    private String url = "https://dahuixiong.oss-cn-shenzhen.aliyuncs.com/";
    private String token;
    private TextView tx_de_name, tx_de_id, tx_de_fansfollow,
            tx_de_money, tx_num, tx_de_qianming, tx_age_sex,
            tx_height, tx_location, tx_weight, tx_uss, tx_em,
            tx_pro, tx_updateTime, tx_de_age, nogift, textUserName, textWeChatNumber;
    private ImageView im_de_copy, im_focus, im_isonline, touserop, icon_back, im_de_sex, v5;
    private LinearLayout linearWeChat;
    private RelativeLayout relativeLook;
    private ConstraintLayout constraintTop;
    private Button bt_sixin, btn_shouyeshipin, bt_liwu, bt_dazhaohu;
    private ScrollView scrollView;
    private XBanner xb_de;
    private String mUserid;
    private RecyclerView rc_newop;
    private int userid = -1;
    private int isfocus, block;
    private List<String> list;
    private SingleImageAdapter imadapter;
    private RecyclerView rv_gifts;
    private RvgiftAdapter adapter;
    private TextView now_coin, open_album, gohei, cancel;
    private Button btn_sendgift;
    private RecyclerView rv_giftsid;
    private GiftsAdapter adaptergifts;
    private ImageView im_de_zhenren;
    private TextView zuixindongtai;
    private ImageView img_sandian, v2;
    private int bannersize;
    private ImageView img_huiyuandegji, v6;
    private GrbqAdapter adaptergrbq;
    private RecyclerView rc_biaoqian1, rc_pingjiabiaoqian;
    private YhpjAdapter yhpjAdapter;
    private TextView rc_yinxiang, yonghuyinxiang, shoudaoliwu;
    private int call;
    private int show;

    private final MutableLiveData<Integer> weChatStatus = new MutableLiveData();
    private final int NO_DISPLAY = 0;
    private final int NORMAL = 1;
    private final int DISPLAY = 2;
    private final int NO_ABLE = 3;
    private UserHome1Bean bean;

    private WeChatDialog weChatDialog;

    @Override
    protected void initListener() {
        tosixin(userid);
        weChatStatus.observe(this, integer -> {
            switch (integer) {
                case NO_DISPLAY:
                    linearWeChat.setVisibility(View.GONE);
                    break;
                case NORMAL:
                    linearWeChat.setVisibility(View.VISIBLE);
                    break;
                case DISPLAY:
                    linearWeChat.setVisibility(View.VISIBLE);
                    relativeLook.setVisibility(View.GONE);
                    textWeChatNumber.setText("微信号:" + bean.getData().getUser().getWxNo());
                    break;
                case NO_ABLE:
                    break;
            }
        });
        relativeLook.setOnClickListener(view -> {
            if (weChatStatus.getValue() == NORMAL) {
                weChatDialog.setCoinNum(bean.getData().getUser().getWxCoin(), true);
                weChatDialog.show();
            } else {
                weChatDialog.setCoinNum(bean.getData().getUser().getWxCoin(), false);
                weChatDialog.show();
            }
        });
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            scrollView.setOnScrollChangeListener((v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
                if (scrollY <= 0) {
                    constraintTop.setBackgroundColor(Color.parseColor("#00FFFFFF"));
                    img_sandian.setImageDrawable(ContextCompat.getDrawable(DetailsActivity.this, R.mipmap.icon_sandianwhite));
                    icon_back.setImageDrawable(ContextCompat.getDrawable(DetailsActivity.this, R.mipmap.ic_backwhite));
                    textUserName.setVisibility(View.GONE);
                } else {
                    constraintTop.setBackgroundColor(Color.parseColor("#FFFFFF"));
                    img_sandian.setImageDrawable(ContextCompat.getDrawable(DetailsActivity.this, R.mipmap.icon_sandian));
                    icon_back.setImageDrawable(ContextCompat.getDrawable(DetailsActivity.this, R.mipmap.ic_back));
                    textUserName.setVisibility(View.VISIBLE);
                }
                Log.d("Agx", scrollY + "");
            });
        }
        btn_shouyeshipin.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                //点击跳转视频通话
                getimPre(EXTRA, userid + "", token);
            }

            @Override
            protected void onFastClick() {

            }
        });

        bt_liwu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GiftDialog giftDialog = new GiftDialog(DetailsActivity.this, userid + "", 3, "0");
                new XPopup.Builder(DetailsActivity.this)
                        .moveUpToKeyboard(true)
                        .asCustom(giftDialog)
                        .show();
                giftDialog.setOnSendSuccess(giftMessageBean -> {
                    MsgAttachment attachment = new GiftAttachment(giftMessageBean);
                    IMMessage customMessage =
                            MessageBuilder.createCustomMessage(String.valueOf(userid), SessionTypeEnum.P2P, getString(R.string.mi_liwuxiaoxi), attachment);
                    NIMClient.getService(MsgService.class).sendMessage(customMessage, false);
                    return null;
                });
            }
        });
        img_sandian.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                showBottomSheetDialog();
            }

            @Override
            protected void onFastClick() {

            }
        });
        bt_dazhaohu.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                //点击打招呼
                sayhello(MmkvUtil.INSTANCE.decodeString(TOKEN), EXTRA, userid);
            }

            @Override
            protected void onFastClick() {

            }
        });
    }

    private void sayhello(String token, String extra, int userid) {
        NetUtils2.getInstance().getApiService().sayHello(token, extra, userid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        LogUtils.i("decryptStrdazhaohu", decryptStr);
                        Gson gson = new Gson();
                        GetMessageBean getMessageBean = gson.fromJson(decryptStr, GetMessageBean.class);
                        if (getMessageBean.getCode() == 0) {
                            bt_dazhaohu.setVisibility(View.GONE);
                            ToastUtils.showShort("打招呼成功");
                        } else {
                            ToastUtils.showShort(getMessageBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 控制底部导航栏的方法
     */
    public void showBottomSheetDialog() {
        Bottomsheet6Dialog tipDialog6 = new Bottomsheet6Dialog(DetailsActivity.this);
        if (block == 0) {
            tipDialog6.setCancel(getString(R.string.mi_lahei));
        } else {
            tipDialog6.setCancel(getString(R.string.mi_quxiaolahei));

        }
        tipDialog6.setOnCancelClickListener(new Function0<Unit>() {
            @Override
            public Unit invoke() {
                if (block == 0) {
                    TipDialog tipDialog = new TipDialog(DetailsActivity.this);
                    tipDialog.setTitle(getString(R.string.mi_caozuotishi));
                    tipDialog.setCancel(getString(R.string.mi_quxiao));
                    tipDialog.setContent(getString(R.string.mi_lahei_dialog_content));
                    tipDialog.setConfirm(getString(R.string.mi_queding));
                    tipDialog.setOnCancelClickListener(new Function0<Unit>() {
                        @Override
                        public Unit invoke() {
                            tipDialog.dismiss();
                            return null;
                        }
                    });
                    tipDialog.setOnConfirmClickListener(new Function0<Unit>() {
                        @Override
                        public Unit invoke() {
                            if (block == 0) {
                                block = 1;
                                tipDialog6.setCancel(getString(R.string.mi_quxiaolahei));
                                getBlocktorf(EXTRA, token, userid, 1);
                            } else {
                                block = 0;
                                tipDialog6.setCancel(getString(R.string.mi_lahei));
                                getBlocktorf(EXTRA, token, userid, 0);
                            }
                            tipDialog.dismiss();
                            return null;


                        }
                    });
                    new XPopup.Builder(DetailsActivity.this)
                            .asCustom(tipDialog)
                            .show();
                } else {
                    if (block == 0) {
                        block = 1;
                        tipDialog6.setCancel(getString(R.string.mi_quxiaolahei));
                        getBlocktorf(EXTRA, token, userid, 1);
                    } else {
                        block = 0;
                        tipDialog6.setCancel(getString(R.string.mi_lahei));
                        getBlocktorf(EXTRA, token, userid, 0);
                    }
                }
                return null;
            }
        });
        tipDialog6.setOnjubaoClickListner(new Function0<Unit>() {
            @Override
            public Unit invoke() {
                Intent intent = new Intent(DetailsActivity.this, ReportActivity.class);
                intent.putExtra("userId", userid);
                startActivity(intent);
                tipDialog6.dismiss();
                return null;
            }
        });
        tipDialog6.setQuxiaoClickListner(new Function0<Unit>() {
            @Override
            public Unit invoke() {
                tipDialog6.dismiss();
                return null;
            }
        });
        new XPopup.Builder(DetailsActivity.this)
                .asCustom(tipDialog6)
                .show();
    }


    /**
     * 拉黑/取消拉黑
     */
    private void getBlocktorf(String extra, String token, int toUserId, int block) {
        NetUtils.getInstance().getApiService().getImblock(extra, token, toUserId, block)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        LogUtils.i("decryptStr42", decryptStr);

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 获取拉黑状态
     *
     * @param extra
     * @param token
     * @param toUserId
     */
    private void getBlock(String extra, String token, int toUserId) {
        NetUtils.getInstance().getApiService().getBlock(extra, token, toUserId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        LogUtils.i("拉黑状态", decryptStr);
                        Gson gson = new Gson();
                        BlockBean bean = gson.fromJson(decryptStr, BlockBean.class);
                        if (bean != null) {
                            if (bean.getData() != null) {
                                block = bean.getData().getBlock();
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }


    private void getimPre(String extra, String toUserId, String token) {
        NetUtils.getInstance().getApiService().getimPre(extra, toUserId, token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        com.blankj.utilcode.util.LogUtils.i("decryptStr10", decryptStr);
                        Gson gson = new Gson();
                        OurinfoBean bean = gson.fromJson(decryptStr, OurinfoBean.class);
                        TestActivity.ourinfoBean = bean;
                        if (bean.getCode() == 0) {
                            //可以发起通话
                            int accId = com.luoxiang.lib_common.utils.MmkvUtil.INSTANCE.decodeInt(ACCID);
                            String id = UUID.randomUUID().toString().replace("-", "");
                            CallParam param = CallParam.createSingleCallParam(2, accId + "", userid + "", null, null, id);
                            CallKitUI.startSingleCall(DetailsActivity.this, param);
                        } else {
                            ToastUtils.showShort(bean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    private void tosixin(int userid) {
        icon_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        bt_sixin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (userid != -1) {
                    if (mUserid.equals(String.valueOf(userid))) {
                        ToastUtils.showShort(R.string.mi_bunenghezijiliaotian);
                    } else {
                        XKitRouter.withKey(RouterConstant.PATH_CHAT_P2P_PAGE).withParam(RouterConstant.CHAT_KRY, new UserInfo(userid + "", "aaa", "")).withContext(DetailsActivity.this).navigate();
                    }
                }
            }
        });
        im_de_copy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ClipboardUtils.copyText(tx_de_id.getText());
                ToastUtils.showShort(R.string.mi_copy_success);
            }
        });
        touserop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(DetailsActivity.this, UserOperationsActivity.class);
                intent.putExtra("name", tx_de_name.getText().toString());
                intent.putExtra("userid", userid);
                startActivity(intent);
            }
        });
        im_focus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isfocus == 0) {
                    isfocus = 1;
                    focus(EXTRA, token, userid, 1);
                    isguanzhu();
                } else {
                    isfocus = 0;
                    focus(EXTRA, token, userid, 0);
                    isguanzhu();
                }
            }
        });
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        setContentView(R.layout.activity_details);
        setStatusBarTranslucent();
    }

    @Override
    protected void initView() {
        showLoading();
        linearWeChat = findViewById(R.id.linearWeChat);
        relativeLook = findViewById(R.id.relativeLook);
        textWeChatNumber = findViewById(R.id.textWeChatNumber);
        tx_de_name = findViewById(R.id.tx_de_name);
        rv_giftsid = findViewById(R.id.recyclerview_gifts);
        tx_de_id = findViewById(R.id.tx_de_id);
        tx_de_fansfollow = findViewById(R.id.tx_de_fansfollow);
        tx_de_money = findViewById(R.id.tx_de_money);
        tx_num = findViewById(R.id.xb_num);
        v5 = findViewById(R.id.v5);
        tx_de_qianming = findViewById(R.id.tx_de_qianming);
        tx_age_sex = findViewById(R.id.tx_age_sex);
        v6 = findViewById(R.id.v6);
        tx_height = findViewById(R.id.tx_height);
        tx_location = findViewById(R.id.tx_location);
        shoudaoliwu = findViewById(R.id.shoudaoliwu);
        tx_weight = findViewById(R.id.tx_weight);
        tx_uss = findViewById(R.id.tx_uss);
        tx_em = findViewById(R.id.tx_em);
        tx_pro = findViewById(R.id.tx_pro);
        tx_de_age = findViewById(R.id.tx_de_age);
        im_de_sex = findViewById(R.id.im_de_sex);
        tx_updateTime = findViewById(R.id.tx_updateTime);
        rc_biaoqian1 = findViewById(R.id.rc_biaoqian1);
        rc_pingjiabiaoqian = findViewById(R.id.rc_pingjiabiaoqian);
        yonghuyinxiang = findViewById(R.id.yonghuyinxiang);
        rc_yinxiang = findViewById(R.id.rc_yinxiang);
        im_de_copy = findViewById(R.id.im_de_copy);
        im_focus = findViewById(R.id.im_focus);
        im_isonline = findViewById(R.id.im_isonline);
        icon_back = findViewById(R.id.icon_back);
        touserop = findViewById(R.id.touserop);
        bt_sixin = findViewById(R.id.bt_sixin);
        rc_newop = findViewById(R.id.rc_newop);
        img_sandian = findViewById(R.id.img_sandian);
        zuixindongtai = findViewById(R.id.zuixindongtai);
        img_huiyuandegji = findViewById(R.id.img_huiyuandegji);
        scrollView = findViewById(R.id.scrollView);
        xb_de = findViewById(R.id.xb_de);
        im_de_zhenren = findViewById(R.id.im_de_zhenren);
        bt_liwu = findViewById(R.id.bt_liwu);
        v2 = findViewById(R.id.v2);
        nogift = findViewById(R.id.nogift);
        bt_dazhaohu = findViewById(R.id.bt_dazhaohu);
        btn_shouyeshipin = findViewById(R.id.btn_shouyeshipin);
        constraintTop = findViewById(R.id.constraintTop);
        textUserName = findViewById(R.id.textUserName);
        token = com.luoxiang.lib_common.utils.MmkvUtil.INSTANCE.decodeString(TOKEN);
        mUserid = MmkvUtil.INSTANCE.decodeString(ACCID);
        Intent intent = getIntent();
        userid = intent.getIntExtra("userid", -1);
        tx_de_id.setText("ID:" + userid);
        weChatDialog = new WeChatDialog(this);
        weChatDialog.setOnClose(() -> {
            return null;
        });
        weChatDialog.setOnPay(aBoolean -> {
            if (aBoolean) {
                unlockWechat(bean.getData().getUser().getUserId());
            } else {
                toast("金币不足");
                Intent intent1 = new Intent(DetailsActivity.this, WalletActivity.class);
                startActivity(intent1);
            }
            return null;
        });
        getUserInfo(token, EXTRA, userid);
        //调用是否关注
        getwhetherFocus(EXTRA, userid, token);
        getUserHome(token, EXTRA, userid);
        getUserDynamic(token, EXTRA, userid);

        getBlock(EXTRA, token, userid);
    }

    /**
     * 解锁微信号
     *
     * @param id
     */
    private void unlockWechat(int id) {
        NetUtils2.getInstance().getApiService().unlockWechat(EXTRA, token, id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(NumberBean value) {
                        String data = value.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        LogUtils.i("decryptStr23", decryptStr);
                        Gson gson = new Gson();
                        RewardBean bean = gson.fromJson(decryptStr, RewardBean.class);
                        if (bean.getCode() == 1) {
                            toast(bean.getMsg());
                            weChatDialog.hide();
                            return;
                        }
                        weChatDialog.dismiss();
                        weChatStatus.setValue(DISPLAY);
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }

    private void getUserDynamic(String token, String extra, int userid) {
        NetUtils2.getInstance().getApiService().getUserDynamic(token, extra, userid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        LogUtils.i("动态", decryptStr);
                        Gson gson = new Gson();
                        UserDynamicBean bean = gson.fromJson(decryptStr, UserDynamicBean.class);
                        if (bean == null || bean.getData() == null) return;
                        if (bean.getData() != null) {
                            if (bean.getData().getDynamics() != null) {
                                if (!bean.getData().getDynamics().isEmpty()) {
                                    LinearLayoutManager layoutManager = new LinearLayoutManager(DetailsActivity.this);
                                    layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                                    rc_newop.setLayoutManager(layoutManager);
                                    imadapter = new SingleImageAdapter(bean.getData().getDynamics());
                                    rc_newop.setAdapter(imadapter);
                                } else {
                                    zuixindongtai.setVisibility(View.GONE);
                                    touserop.setVisibility(View.GONE);
                                    rc_newop.setVisibility(View.GONE);
                                    v2.setVisibility(View.GONE);
                                }

                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void getUserHome(String token, String extra, int userid) {
        NetUtils2.getInstance().getApiService().getUserHome(token, extra, userid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        clearLoading();
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        LogUtils.i("主播信息", decryptStr);
                        Gson gson = new Gson();
                        bean = gson.fromJson(decryptStr, UserHome1Bean.class);
                        if (bean == null || bean.getData() == null) return;
                        if (bean.getData() != null) {
                            if (bean.getData().getUser() != null) {
                                if (bean.getData().getUser().getWxShow() == 1) {
                                    if (bean.getData().getUser().getWxNo() == null || bean.getData().getUser().getWxNo().isEmpty()) {
                                        weChatStatus.setValue(NO_DISPLAY);
                                    } else {
                                        weChatStatus.setValue(DISPLAY);
                                        textWeChatNumber.setText("微信号:" + bean.getData().getUser().getWxCoin() + "金币");
                                        if (MmkvUtil.INSTANCE.decodeInt(HOSTAUDIT) != null && MmkvUtil.INSTANCE.decodeInt(HOSTAUDIT) == 0) {
                                            if (bean.getData().getUser().getBuyWxCode() == 1) {
                                                weChatStatus.setValue(DISPLAY);
                                            } else {
                                                weChatStatus.setValue(NORMAL);
                                                if (bean.getData().getUser().getCoinCode() != 1) {
                                                    weChatStatus.setValue(NO_ABLE);
                                                }
                                            }
                                        } else {
                                            weChatStatus.setValue(NO_DISPLAY);
                                        }
                                    }
                                } else {
                                    weChatStatus.setValue(NO_DISPLAY);
                                }

                                tx_de_fansfollow.setText(bean.getData().getUser().getFoucsNum() + getString(R.string.mi_guanzhu2) + bean.getData().getUser().getFansNum() + getString(R.string.mi_fensi));

                                call = bean.getData().getUser().getCall();
                                if (call == 0 && show == 1) {
                                    //表示未打招呼
                                    bt_dazhaohu.setVisibility(View.VISIBLE);
                                } else {
                                    bt_dazhaohu.setVisibility(View.GONE);
                                }
                            }
                        }
                        if (bean.getData().getUser().getName() != null) {
                            tx_de_name.setText(bean.getData().getUser().getName());
                            textUserName.setText(bean.getData().getUser().getName());
                        }
                        if (bean.getData().getUser().getFoucsNum() != 0) {


                        }
                        if (bean.getData().getUser().getCharge() != 0) {
                            tx_de_money.setText(getString(R.string.mi_shipintonghua) + bean.getData().getUser().getCharge() + getString(R.string.mi_coin));
                        }
                        if (bean.getData().getUser().getPersionSign() != null) {
                            tx_de_qianming.setText(bean.getData().getUser().getPersionSign());
                        }
                        if (bean.getData().getUser().getOnline() == 2) {
                            im_isonline.setImageResource(R.mipmap.icon_zailiao);
                            im_isonline.setVisibility(View.VISIBLE);

                        } else if (bean.getData().getUser().getOnline() == 3) {
                            im_isonline.setImageResource(R.mipmap.icon_online);
                            im_isonline.setVisibility(View.VISIBLE);
                        } else {
                            im_isonline.setVisibility(View.GONE);
                        }
                        if (xb_de != null) {
                            if (!bean.getData().getUser().getPhotos().isEmpty()) {
                                if (bean.getData().getUser().getPhotos().get(0).getIcon().contains(".mp4") || bean.getData().getUser().getPhotos().get(0).getIcon().contains(".gif")) {
                                    bean.getData().getUser().getPhotos().remove(0);

                                    bannersize = bean.getData().getUser().getPhotos().size();
                                    LogUtils.i("bannersize", bannersize);
                                    tx_num.setText("1/" + bannersize);
                                }
                                xb_de.setData(bean.getData().getUser().getPhotos(), null);
                                xb_de.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                                    @Override
                                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                                    }

                                    @Override
                                    public void onPageSelected(int position) {
                                        if (bean.getData() != null && bean.getData().getUser() != null) {
                                            tx_num.setText((position + 1) + "/" + bean.getData().getUser().getPhotos().size());
                                        }
                                    }

                                    @Override
                                    public void onPageScrollStateChanged(int state) {

                                    }
                                });
                                xb_de.setmAdapter(new XBanner.XBannerAdapter() {
                                    @Override
                                    public void loadBanner(XBanner banner, View view, int position) {
                                        RequestOptions options = new RequestOptions()
                                                .placeholder(R.mipmap.icon_error_banner)
                                                .fallback(R.mipmap.icon_error_banner)
                                                .error(R.mipmap.icon_error_banner);
                                        Glide.with(DetailsActivity.this)
                                                .load(url + bean.getData().getUser().getPhotos().get(position).getIcon())
                                                .apply(options)
                                                .into((ImageView) view);
                                        ImageView imageView = (ImageView) view;
                                        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                                    }
                                });

                                xb_de.setPageTransformer(Transformer.Default);
                            }

                        }


                    }

                    @Override
                    public void onError(Throwable e) {
                        clearLoading();
                    }

                    @Override
                    public void onComplete() {
                        clearLoading();
                    }
                });
    }

    private void isguanzhu() {
        //是否关注
        if (isfocus == 0) {
            //未关注
            im_focus.setImageResource(R.mipmap.icon_nofollow_de);
        } else {
            //已关注
            im_focus.setImageResource(R.mipmap.icon_follow_de);

        }
    }

    //关注的方法
    private void focus(String extra, String token, int toUserId, int type) {
        NetUtils.getInstance().getApiService().getfocus(extra, token, toUserId, type)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        com.blankj.utilcode.util.LogUtils.i("decryptStr10", decryptStr);
                        Gson gson = new Gson();
                        GetMessageBean getWhereBean = gson.fromJson(decryptStr, GetMessageBean.class);
                        if (getWhereBean == null) return;
                        if (getWhereBean.getCode() != 0) {
                            ToastUtils.showShort(getWhereBean.getMsg());
                            getwhetherFocus(EXTRA, userid, token);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void getwhetherFocus(String extra, int userId, String token) {
        NetUtils.getInstance().getApiService().getwhetherFocus(extra, userId, token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        clearLoading();
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        com.blankj.utilcode.util.LogUtils.i("decryptStr9", decryptStr);
                        Gson gson = new Gson();
                        GetWhereBean getWhereBean = gson.fromJson(decryptStr, GetWhereBean.class);
                        if (getWhereBean != null) {
                            if (getWhereBean.getData() != null) {
                                isfocus = getWhereBean.getData().getFlag();
                                show = getWhereBean.getData().isShow();
                                if (show != 1) {
                                    //隐藏处理
                                    im_focus.setVisibility(View.GONE);
                                    bt_sixin.setVisibility(View.GONE);
                                    bt_liwu.setVisibility(View.GONE);
                                    bt_dazhaohu.setVisibility(View.GONE);
                                    btn_shouyeshipin.setVisibility(View.GONE);
                                } else {
                                    //展示处理
                                    im_focus.setVisibility(View.VISIBLE);
                                    bt_sixin.setVisibility(View.VISIBLE);
                                    bt_liwu.setVisibility(View.VISIBLE);
                                    btn_shouyeshipin.setVisibility(View.VISIBLE);
                                    bt_dazhaohu.setVisibility(View.VISIBLE);

                                }
                            }
                        }
                        isguanzhu();
                    }

                    @Override
                    public void onError(Throwable e) {
                        clearLoading();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        Intent intent = getIntent();
        userid = intent.getIntExtra("userid", -1);
//        tx_num.setText("1" + "/" + bannersize);
    }

    private void getUserInfo(String token, String extra, int userid) {
        NetUtils2.getInstance().getApiService().getUserInfo(token, extra, userid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        LogUtils.i("用户信息", decryptStr);
                        Gson gson = new Gson();
                        UserInfo1Bean bean = gson.fromJson(decryptStr, UserInfo1Bean.class);
                        if (bean.getData().getUserInfo() != null) {
                            if (bean.getData().getUserInfo().getLables() != null) {
                                if (bean.getData().getUserInfo().getLables().size() > 0) {
                                    adaptergrbq = new GrbqAdapter(bean.getData().getUserInfo().getLables());
                                    GridLayoutManager gridLayoutManager = new GridLayoutManager(DetailsActivity.this, 4);
                                    rc_biaoqian1.setAdapter(adaptergrbq);
                                    rc_biaoqian1.setLayoutManager(gridLayoutManager);
                                }
                            }
                        }

                        if (bean.getData().getUserInfo().getUserLables() != null) {
                            if (!bean.getData().getUserInfo().getUserLables().isEmpty()) {
                                yhpjAdapter = new YhpjAdapter(bean.getData().getUserInfo().getUserLables());
                                rc_yinxiang.setVisibility(View.GONE);
                                v5.setVisibility(View.VISIBLE);
                                GridLayoutManager gridLayoutManager = new GridLayoutManager(DetailsActivity.this, 3);
                                rc_pingjiabiaoqian.setAdapter(yhpjAdapter);
                                rc_pingjiabiaoqian.setLayoutManager(gridLayoutManager);

                            } else {
                                rc_yinxiang.setVisibility(View.GONE);
                                yonghuyinxiang.setVisibility(View.GONE);
                                v5.setVisibility(View.GONE);

                            }

                        }

                        if (bean.getData() != null) {
                            if (bean.getData().getUserInfo() != null) {
                                int identity = bean.getData().getUserInfo().getIdentity();
                                int host = MmkvUtil.INSTANCE.decodeInt(HOST);
                                if (host == 1) {
                                    tx_de_money.setVisibility(View.GONE);
                                    if (identity == 0) {
                                        img_huiyuandegji.setVisibility(View.INVISIBLE);
                                    } else if (identity == 10) {
                                        img_huiyuandegji.setImageResource(R.mipmap.icon_typehuangjin);
                                        img_huiyuandegji.setVisibility(View.VISIBLE);
                                    } else if (identity == 20) {
                                        img_huiyuandegji.setImageResource(R.mipmap.icon_typebojin);
                                        img_huiyuandegji.setVisibility(View.VISIBLE);
                                    } else if (identity == 30) {
                                        img_huiyuandegji.setImageResource(R.mipmap.icon_typezuanshi);
                                        img_huiyuandegji.setVisibility(View.VISIBLE);
                                    }
                                } else {
                                    img_huiyuandegji.setVisibility(View.GONE);
                                    tx_de_money.setVisibility(View.VISIBLE);
                                }

                                if (bean.getData().getUserInfo().getCity() != null) {
                                    tx_location.setText(getString(R.string.mi_suozaidi) + bean.getData().getUserInfo().getCity());
                                    tx_location.setVisibility(View.VISIBLE);
                                }
                                if (bean.getData().getUserInfo().getEm() != null) {
                                    tx_em.setText(bean.getData().getUserInfo().getEm());
                                    tx_em.setVisibility(View.VISIBLE);
                                }
                                if (bean.getData().getUserInfo().getWeight() != 0) {
                                    tx_weight.setText(bean.getData().getUserInfo().getWeight() + "kg");
                                    tx_weight.setVisibility(View.VISIBLE);
                                }
                                if (bean.getData().getUserInfo().getHeight() != 0) {
                                    tx_height.setText(bean.getData().getUserInfo().getHeight() + "cm");
                                    tx_height.setVisibility(View.VISIBLE);
                                }
                                if (bean.getData().getUserInfo().getUss() != null) {
                                    tx_uss.setText(bean.getData().getUserInfo().getUss());
                                    tx_uss.setVisibility(View.VISIBLE);
                                }
                                if (bean.getData().getUserInfo().getPro() != null) {
                                    tx_pro.setText(bean.getData().getUserInfo().getPro());
                                    tx_pro.setVisibility(View.VISIBLE);
                                }
                                if (bean.getData().getUserInfo().getUpdateTime() != null) {
                                    tx_updateTime.setText(getString(R.string.mi_shangcidenglu) + bean.getData().getUserInfo().getUpdateTime());
                                    tx_updateTime.setVisibility(View.GONE);
                                }
                                if (bean.getData().getUserInfo().getGender() == 1) {
                                    tx_age_sex.setText(getString(R.string.mi_nan_t) + bean.getData().getUserInfo().getAge() + getString(R.string.mi_sui));
                                    im_de_sex.setImageResource(R.mipmap.icon_nansheng);
                                    tx_de_age.setText(bean.getData().getUserInfo().getAge() + "");
                                    tx_age_sex.setVisibility(View.VISIBLE);
                                    tx_de_age.setBackgroundResource(R.drawable.shape_tc_txbg);
                                } else if (bean.getData().getUserInfo().getGender() == 0) {
                                    tx_age_sex.setText(getString(R.string.mi_nv_t) + bean.getData().getUserInfo().getAge() + getString(R.string.mi_sui));
                                    tx_de_age.setText(bean.getData().getUserInfo().getAge() + "");
                                    im_de_sex.setImageResource(R.mipmap.icon_nvsheng);
                                    tx_age_sex.setVisibility(View.VISIBLE);
                                    tx_de_age.setBackgroundResource(R.drawable.shape_girlbg);
                                } else if (bean.getData().getUserInfo().getGender() == 2) {
                                    tx_age_sex.setText(getString(R.string.mi_nv_t) + bean.getData().getUserInfo().getAge() + getString(R.string.mi_sui));
                                    tx_age_sex.setVisibility(View.VISIBLE);
                                    im_de_sex.setImageResource(R.mipmap.icon_nvsheng);
                                    tx_de_age.setBackgroundResource(R.drawable.shape_girlbg);
                                }
                                if (bean.getData().getUserInfo().getHost() == 1) {
                                    im_de_zhenren.setVisibility(View.VISIBLE);
                                } else {
                                    im_de_zhenren.setVisibility(View.GONE);
                                }

                            }
                            if (bean.getData().getUserInfo() != null) {
                                if (bean.getData().getUserInfo().getGifts() != null && !bean.getData().getUserInfo().getGifts().isEmpty()) {
                                    adaptergifts = new GiftsAdapter(bean.getData().getUserInfo().getGifts());
                                    GridLayoutManager gridLayoutManager = new GridLayoutManager(DetailsActivity.this, 4);
                                    rv_giftsid.setLayoutManager(gridLayoutManager);
                                    rv_giftsid.setAdapter(adaptergifts);
                                    nogift.setVisibility(View.GONE);
                                    shoudaoliwu.setVisibility(View.VISIBLE);
                                    v6.setVisibility(View.VISIBLE);
                                } else {
                                    nogift.setVisibility(View.GONE);
                                    shoudaoliwu.setVisibility(View.GONE);
                                    v6.setVisibility(View.GONE);
                                }
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}